%macro get_receipts(form=);

	proc printto new log = "&logdir./get_recepts_&form..txt";
	run;
	
	%if "&form" = "f1120" %then %do;
		%let recvar = [redacted];
		%let tinvar = [redacted];
		%let cdwname = [redacted];
		%let incvar = [redacted];
	%end;
	%if "&form" = "f1120s" %then %do;
		%let recvar = [redacted];
		%let tinvar = [redacted];
		%let cdwname = [redacted];
		%let incvar = [redacted];
	%end;
	%if "&form" = "f1065" %then %do;
		%let recvar = [redacted];
		%let tinvar = [redacted];
		%let cdwname = [redacted];
		%let incvar = coalesce([redacted],0) +
			coalesce([redacted],0) + 
			coalesce([redacted],0) + 
			coalesce([redacted],0) +
			coalesce([redacted],0) +
			coalesce([redacted],0) +
			coalesce([redacted],0) +
			coalesce([redacted],0) +
			coalesce([redacted],0);
	%end;
	%if "&form" = "sched_c" %then %do;
		%let recvar = [redacted];
		%let tinvar = [redacted];
		%let cdwname = [redacted];
		%let incvar = [redacted];
	%end;


		/************************************************
		*												*
		*					Main SQL call				*
		*												*
		************************************************/
		options errors = 0;
				proc sql noprint;
				[redacted] (

						select
							max(&recvar) as receipts,
							max(&incvar) as income,
							case when [redacted] <= 202006 then 2019 else 2020 end as year,
							&tinvar as firm_id
						from &cdwname
						where [redacted] between 201907 and 202106
							and &tinvar ~= [redacted]
						group by &tinvar, year
						order by &tinvar, year



					);
				disconnect from iq;
				quit;

		
		data d2019;
			set sample(where=(year=2019));
			receipts2019_&form = receipts;
			income2019_&form = income;
			keep firm_id receipts2019_&form income2019_&form;
		run;
		
		data d2020;
			set sample(where=(year=2020));
			receipts2020_&form = receipts;
			income2020_&form = income;
			keep firm_id receipts2020_&form income2020_&form;
		run;
		
		data rlib.receipts_&form;
			merge d2019 d2020;
			by firm_id;
		run;
	proc printto;
	run;
	

%mend;
